

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Cheney">
  <meta name="keywords" content="Coding">
  
    <meta name="description" content="使用管道来实现父子进程之间的进程通信，使用共享存储区来实现两个进程之间的进程通信，使用消息缓冲队列来实现 client 进程和 server 进程之间的通信。">
<meta property="og:type" content="article">
<meta property="og:title" content="进程通信的三种方式（管道、消息缓冲队列、共享缓冲区）">
<meta property="og:url" content="https://cheney822.gitee.io/2021/05/06/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F__%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1%E7%9A%84%E4%B8%89%E7%A7%8D%E6%96%B9%E5%BC%8F%EF%BC%88%E7%AE%A1%E9%81%93%E3%80%81%E6%B6%88%E6%81%AF%E7%BC%93%E5%86%B2%E9%98%9F%E5%88%97%E3%80%81%E5%85%B1%E4%BA%AB%E7%BC%93%E5%86%B2%E5%8C%BA%EF%BC%89/index.html">
<meta property="og:site_name" content="Cheney blog">
<meta property="og:description" content="使用管道来实现父子进程之间的进程通信，使用共享存储区来实现两个进程之间的进程通信，使用消息缓冲队列来实现 client 进程和 server 进程之间的通信。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://imgbed.cheney.cc/picgo/20210505112734975.png">
<meta property="article:published_time" content="2021-05-06T00:22:00.000Z">
<meta property="article:modified_time" content="2022-04-05T13:14:29.763Z">
<meta property="article:author" content="Cheney">
<meta property="article:tag" content="算法">
<meta property="article:tag" content="C语言">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://imgbed.cheney.cc/picgo/20210505112734975.png">
  
  
  <title>进程通信的三种方式（管道、消息缓冲队列、共享缓冲区） - Cheney blog</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"cheney822.gitee.io","root":"/","version":"1.8.14","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":1},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.1"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Cheney Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('https://imgbed.cheney.cc/Blog_config/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="进程通信的三种方式（管道、消息缓冲队列、共享缓冲区）">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-05-06 08:22" pubdate>
        2021年5月6日 早上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      7.2k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      36 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">进程通信的三种方式（管道、消息缓冲队列、共享缓冲区）</h1>
            
            <div class="markdown-body">
              <h2 id="背景：（关于进程通信的几种方式）"><a href="#背景：（关于进程通信的几种方式）" class="headerlink" title="背景：（关于进程通信的几种方式）"></a>背景：（关于进程通信的几种方式）</h2><h2 id="题目描述："><a href="#题目描述：" class="headerlink" title="题目描述："></a>题目描述：</h2><p>编写一主程序可以由用户选择如下三种进程通信方式：</p>
<ol>
<li><strong>使用管道来实现父子进程之间的进程通信</strong><br>子进程向父进程发送自己的进程标识符，以及字符串“is sending a message to parent”。父进程则通过管道读出子进程发来的消息，将消息显示在屏幕上，然后终止。</li>
<li><strong>使用共享存储区来实现两个进程之间的进程通信</strong><br>进程 A 创建一个长度为 512 字节的共享内存，并显示写入该共享内存的数据；进程 B 将共享内存附加到自己的地址空间，并向共享内存中写入数据。</li>
<li><strong>使用消息缓冲队列来实现 client 进程和 server 进程之间的通信</strong><br>server 进程先建立一个关键字为 SVKEY（如 75）的消息队列，然后等待接收类型为 REQ（例如 1）的消息；<br>在收到请求消息后，它便显示字符串“serving for client”和接收到的 client 进程的进程标识数，表示正在为 client 进程服务；<br>然后再向 client 进程发送应答消息，该消息的类型是 client 进程的进程标识数，而正文则是 server 进程自己的标识ID。<br>client 进程则向消息队列发送类型为 REQ 的消息（消息的正文为自己的进程标识 ID） 以取得 sever 进程的服务，并等待 server 进程发来的应答；<br>然后显示字符串“receive reply from”和接收到的 server 进程的标识 ID。<h2 id="算法设计："><a href="#算法设计：" class="headerlink" title="算法设计："></a>算法设计：</h2><h3 id="管道："><a href="#管道：" class="headerlink" title="管道："></a>管道：</h3>对管道的设计类似于上一篇文章：<a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_46291251/article/details/116274665">Linux下两个子进程通过管道（pipe）通信</a><h3 id="共享缓冲区："><a href="#共享缓冲区：" class="headerlink" title="共享缓冲区："></a>共享缓冲区：</h3>共享内存是操作系统常采用的进程间通信方式。它使得多个进程可以访问同一块内存空间，不同进程可以及时看到对方进程中对共享内存中数据的更新。这种通信方式需要依靠某种同步机制，如互斥锁和信号量等。<h4 id="共享内存通过系统调用-shmget-、shmat-、shmdt-、shmctl-来实现"><a href="#共享内存通过系统调用-shmget-、shmat-、shmdt-、shmctl-来实现" class="headerlink" title="共享内存通过系统调用 shmget()、shmat()、shmdt()、shmctl()来实现."></a>共享内存通过系统调用 shmget()、shmat()、shmdt()、shmctl()来实现.</h4></li>
</ol>
<ul>
<li>系统调用：shmget()<br>原型：<code>int shmget(key_t key, int size, int shmflg)</code><br>参数：<br>Key:共享存储区的名字；<br>Size：共享存储区的大小（以字节计）；<br>Shmflag：用户设置的标志，如 IPC_CREAT，IPC_CREAT 表示若系统中尚无指名的共享存储区，则由核心建立一个共享存储区；如系统中已有共享存储区，便忽略 IPC_CREAT。<br>返回值：<br>成功返回共享内存的标识符；不成功返回-1</li>
<li>系统调用：shmat()<br>原型：<code>void* shmat(int shmid, const void *shmaddr, int shmflg);</code><br>参数：<br>Shmid：共享存储区的标识符；<br>Shmaddr：用户给定的，将共享存储区附接到进程的虚拟地址空间；<br>Shmflg：规定共享存储区的读、写权限，以及系统是否应对用户规定的地址做舍入操作。其值为 SHM_RDONLY 时，表示只能读，其值为 0 时，表示可读、可写，其值为 SHM_RND（取整）时，表示操作系统在必要时舍去这个地址。返回值：<br>该系统调用的返回值为共享存储区所附接到的进程虚地址。</li>
<li>系统调用：shmdt()<br>原型：int shmdt(void *shmaddr)参数：<br>Shmaddr：要断开连接的虚地址，亦即以前由连接的系统调用 shmat（）所返回的虚地址。<br>返回值：<br>调用成功时，返回 0 值，调用不成功，返回-1 值。</li>
<li>系统调用：shmctl()<br>原型：int shmctl(int shmid, int cmd, struct shmid_ds *buf);<br>参数：<br>Shmid：由 shmget 所返回的标识符；<br>Cmd：是操作命令，可以分多种类型：<br>（1）用于查询有关共享存储区的情况。如其长度、当前连接的进程数、共享区的创建者标识符。<br>（2）用于设置或改变共享存储区的属性，如共享存储区的许可权、当前连接的进程计数等。<br>（3）对共享存储区的加锁和解锁命令。<br>（4）删除共享存储区标识符等。<br>Buf：用户缓冲区地址。返回值：<br>调用成功则返回 0，如果失败则返回-1。</li>
</ul>
<h4 id="创建共享缓冲区并等待被写入，然后读取："><a href="#创建共享缓冲区并等待被写入，然后读取：" class="headerlink" title="创建共享缓冲区并等待被写入，然后读取："></a>创建共享缓冲区并等待被写入，然后读取：</h4><figure class="highlight cpp"><table><tr><td class="gutter"><div class="code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></div></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-comment">//共享存储区</span><br>        shmid=<span class="hljs-built_in">shmget</span>(SHMKEY,<span class="hljs-number">512</span>,<span class="hljs-number">0777</span>|IPC_CREAT); <span class="hljs-comment">/*创建共享存储区*/</span><br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;进程%d创建了共享存储区:\n&quot;</span>,<span class="hljs-built_in">getpid</span>());<br>        addr=<span class="hljs-built_in">shmat</span>(shmid,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>);        <span class="hljs-comment">/*获取首地址*/</span><br>        *addr=<span class="hljs-number">-1</span>;<br>        <span class="hljs-keyword">while</span> (*addr == <span class="hljs-number">-1</span>);<span class="hljs-comment">//阻塞，用于进程同步</span><br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;进程%d读取共享存储区:%d\n&quot;</span>,<span class="hljs-built_in">getpid</span>(),*addr);<br>   <br></code></pre></td></tr></table></figure>
<h4 id="等待共享缓冲区被创建，然后写入："><a href="#等待共享缓冲区被创建，然后写入：" class="headerlink" title="等待共享缓冲区被创建，然后写入："></a>等待共享缓冲区被创建，然后写入：</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-comment">//共享存储区</span><br>       shmid=<span class="hljs-built_in">shmget</span>(SHMKEY,<span class="hljs-number">512</span>,<span class="hljs-number">0777</span>);      <span class="hljs-comment">/*打开共享存储区*/</span><br>       addr=<span class="hljs-built_in">shmat</span>(shmid,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>);           <span class="hljs-comment">/*获得共享存储区首地址*/</span><br>       <span class="hljs-keyword">while</span>(*addr != <span class="hljs-number">-1</span>);<span class="hljs-comment">//阻塞，用于进程同步</span><br>       <br>       <span class="hljs-type">int</span> xie = <span class="hljs-built_in">getpid</span>();<br>       <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;进程%d写入共享存储区:%d\n&quot;</span>,<span class="hljs-built_in">getpid</span>(),xie);<br>       *addr= xie;<br></code></pre></td></tr></table></figure>


<h3 id="消息缓冲队列："><a href="#消息缓冲队列：" class="headerlink" title="消息缓冲队列："></a>消息缓冲队列：</h3><h4 id="自定义消息数据结构："><a href="#自定义消息数据结构：" class="headerlink" title="自定义消息数据结构："></a>自定义消息数据结构：</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-keyword">struct</span> <span class="hljs-title class_">msgform</span>  <span class="hljs-comment">//消息结构</span><br>&#123;<br>   <span class="hljs-type">long</span> mtype; <br>   <span class="hljs-type">char</span> mtext[<span class="hljs-number">250</span>];  <span class="hljs-comment">//文本长度</span><br>&#125;; <br></code></pre></td></tr></table></figure>
<h4 id="需要用到的函数：-msgget-、msgsnd-、msgrcv"><a href="#需要用到的函数：-msgget-、msgsnd-、msgrcv" class="headerlink" title="需要用到的函数： msgget()、msgsnd()、msgrcv()"></a>需要用到的函数： <code>msgget()、msgsnd()、msgrcv()</code></h4><ul>
<li>原型：<code>int msgget（key_t key，int msgflg）</code><br>参数：<br>key：消息队列关联的键。<br>msgflg：消息队列的建立标志和存取权限。<br>返回值：成功执行时，返回消息队列标识值。失败返回-1，</li>
<li>原型：<code>ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);</code><br>参数：<br>msqid：消息队列的识别码。<br>msgp：指向消息缓冲区的指针，此位置用来暂时存储发送和接收的消息，是一个用户可定义的通用结构。<br>msgsz：消息的大小。<br>msgtyp：从消息队列内读取的消息形态。如果值为零，则表示消息队列中的所有消息都会被读取。<br>msgflg：用来指明核心程序在队列没有数据的情况下所应采取的行动。如果 msgflg 和常数 IPC_NOWAIT 合用，则在消息队列呈空时，不做等待马上返回-1，并设定错误码为ENOMSG。当 msgflg 为 0 时， msgrcv()在队列呈满或呈空的情形时，采取阻塞等待的处理模式。<br>返回值：当成功执行时， msgrcv()返回拷贝到 mtext 数组的实际字节数。失败返回-1，</li>
</ul>
<h4 id="server-端"><a href="#server-端" class="headerlink" title="server() 端"></a>server() 端</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">server</span><span class="hljs-params">()</span> </span><br><span class="hljs-function"></span>&#123;   <br>   <span class="hljs-type">int</span> c_pid,s_pid,msgqid;<span class="hljs-comment">//client的pid 和 消息队列识别码</span><br>   <span class="hljs-keyword">struct</span> <span class="hljs-title class_">msgform</span> msg;<br>   msgqid=<span class="hljs-built_in">msgget</span>(SVKEY,<span class="hljs-number">0777</span>|IPC_CREAT);  <span class="hljs-comment">//创建 75#消息队列 </span><br>   <span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>)&#123;<br>        <span class="hljs-built_in">msgrcv</span>(msgqid,&amp;msg,<span class="hljs-number">250</span>,<span class="hljs-number">1</span>,<span class="hljs-number">0</span>);  <span class="hljs-comment">//接收client进程标识数消息 </span><br>        c_pid=*(<span class="hljs-type">int</span> *)msg.mtext;  <span class="hljs-comment">//获得 cilent 进程标识数</span><br>        <span class="hljs-keyword">if</span>(msg.mtype==REQ)&#123;<br>            <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;(server):serving for client pid=%d\n&quot;</span>,c_pid); <br>            <span class="hljs-keyword">break</span>;<br>        &#125;<br>   &#125;<br>   <br><br>   msg.mtype=c_pid;  <span class="hljs-comment">//消息类型为 client 进程标识数</span><br>   s_pid = <span class="hljs-built_in">getpid</span>();<br>   *(<span class="hljs-type">int</span> *)msg.mtext=s_pid; <span class="hljs-comment">//获取 server 进程标识数 </span><br>   <span class="hljs-comment">//printf(&quot;ser修改后的 mtype=%d，，mtext=%d\n&quot;,(int)msg.mtype,*(int *)msg.mtext); //调试信息</span><br>   <span class="hljs-built_in">msgsnd</span>(msgqid,&amp;msg,<span class="hljs-built_in">sizeof</span>(<span class="hljs-type">int</span>),<span class="hljs-number">0</span>);  <span class="hljs-comment">//发送消息 </span><br>   <span class="hljs-built_in">exit</span>(<span class="hljs-number">0</span>); <br>&#125; <br></code></pre></td></tr></table></figure>

<h4 id="client-端"><a href="#client-端" class="headerlink" title="client() 端"></a>client() 端</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">client</span><span class="hljs-params">()</span> </span><br><span class="hljs-function"></span>&#123; <br>   <span class="hljs-type">int</span> pid,msgqid;<span class="hljs-comment">//client的pid 和 消息队列识别码</span><br>   <span class="hljs-keyword">struct</span> <span class="hljs-title class_">msgform</span> msg;<br>   msgqid=<span class="hljs-built_in">msgget</span>(SVKEY,<span class="hljs-number">0777</span>);  <span class="hljs-comment">//打开 75#消息队列</span><br>   <br>   pid=<span class="hljs-built_in">getpid</span>();  <span class="hljs-comment">//获取client进程标识符</span><br>   *(<span class="hljs-type">int</span> *)msg.mtext=pid;  <span class="hljs-comment">//pint指针指向client进程标识符</span><br>   msg.mtype=REQ;  <span class="hljs-comment">//消息类型为 1</span><br>   <span class="hljs-comment">//printf(&quot;cli修改后的 mtype=%d，，mtext=%d\n&quot;,(int)msg.mtype,*(int *)msg.mtext); //调试信息</span><br><br>   <span class="hljs-built_in">msgsnd</span>(msgqid,&amp;msg,<span class="hljs-built_in">sizeof</span>(<span class="hljs-type">int</span>),<span class="hljs-number">0</span>);  <span class="hljs-comment">//发送消息msg入msgqid消息队列</span><br>   <span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>)&#123;<br>        <span class="hljs-built_in">msgrcv</span>(msgqid,&amp;msg,<span class="hljs-number">250</span>,pid,<span class="hljs-number">0</span>);  <span class="hljs-comment">//从队列msgqid接收消息msg </span><br>        <span class="hljs-keyword">if</span>(msg.mtype!=REQ)&#123;<br>            <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;(client):receive reply from pid=%d\n&quot;</span>,*(<span class="hljs-type">int</span> *)msg.mtext);  <span class="hljs-comment">//显示 server进程标识数</span><br>            <span class="hljs-built_in">exit</span>(<span class="hljs-number">0</span>); <br>        &#125;<br>   &#125;<br>&#125; <br></code></pre></td></tr></table></figure>

<h2 id="实现代码："><a href="#实现代码：" class="headerlink" title="实现代码："></a>实现代码：</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-comment">/*</span><br><span class="hljs-comment">操作系统上机题目6：进程通信</span><br><span class="hljs-comment">1.利用管道实现父子进程的通信</span><br><span class="hljs-comment">2.使用共享存储区实现两个进程的通信</span><br><span class="hljs-comment">    shmget()；//创建共享内存。</span><br><span class="hljs-comment">    shmat();//映射共享内存</span><br><span class="hljs-comment">    shmdt();//撤销映射</span><br><span class="hljs-comment">原型：int shmget(Key_t key,int size,int shmflg);</span><br><span class="hljs-comment">    key:共享内存的键值</span><br><span class="hljs-comment">    size：共享内存大小</span><br><span class="hljs-comment">    shmflg：权限位</span><br><span class="hljs-comment">    返回值：共享内存段标识符</span><br><span class="hljs-comment"></span><br><span class="hljs-comment">3.使用消息缓冲队列实现client和server的通信</span><br><span class="hljs-comment"></span><br><span class="hljs-comment"></span><br><span class="hljs-comment">*/</span><br><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;signal.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;sys/wait.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;stdio.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;unistd.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;stdlib.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;string.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;sys/types.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;sys/ipc.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;sys/shm.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;sys/msg.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">define</span>  SHMKEY  82</span><br><span class="hljs-meta">#<span class="hljs-keyword">define</span>  SVKEY 75 <span class="hljs-comment">//定义关键字SVKEY</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">define</span>  REQ  1</span><br><br><span class="hljs-keyword">struct</span> <span class="hljs-title class_">msgform</span>  <span class="hljs-comment">//消息结构</span><br>&#123;<br>   <span class="hljs-type">long</span> mtype; <br>   <span class="hljs-type">char</span> mtext[<span class="hljs-number">250</span>];  <span class="hljs-comment">//文本长度</span><br>&#125;; <br><br><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">server</span><span class="hljs-params">()</span> </span><br><span class="hljs-function"></span>&#123;   <br>   <span class="hljs-type">int</span> c_pid,s_pid,msgqid;<span class="hljs-comment">//client的pid 和 消息队列识别码</span><br>   <span class="hljs-keyword">struct</span> <span class="hljs-title class_">msgform</span> msg;<br>   msgqid=<span class="hljs-built_in">msgget</span>(SVKEY,<span class="hljs-number">0777</span>|IPC_CREAT);  <span class="hljs-comment">//创建 75#消息队列 </span><br>   <span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>)&#123;<br>        <span class="hljs-built_in">msgrcv</span>(msgqid,&amp;msg,<span class="hljs-number">250</span>,<span class="hljs-number">1</span>,<span class="hljs-number">0</span>);  <span class="hljs-comment">//接收client进程标识数消息 </span><br>        c_pid=*(<span class="hljs-type">int</span> *)msg.mtext;  <span class="hljs-comment">//获得 cilent 进程标识数</span><br>        <span class="hljs-keyword">if</span>(msg.mtype==REQ)&#123;<br>            <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;(server):serving for client pid=%d\n&quot;</span>,c_pid); <br>            <span class="hljs-keyword">break</span>;<br>        &#125;<br>   &#125;<br>   <br><br>   msg.mtype=c_pid;  <span class="hljs-comment">//消息类型为 client 进程标识数</span><br>   s_pid = <span class="hljs-built_in">getpid</span>();<br>   *(<span class="hljs-type">int</span> *)msg.mtext=s_pid; <span class="hljs-comment">//获取 server 进程标识数 </span><br>   <span class="hljs-comment">//printf(&quot;ser修改后的 mtype=%d，，mtext=%d\n&quot;,(int)msg.mtype,*(int *)msg.mtext); //调试信息</span><br>   <span class="hljs-built_in">msgsnd</span>(msgqid,&amp;msg,<span class="hljs-built_in">sizeof</span>(<span class="hljs-type">int</span>),<span class="hljs-number">0</span>);  <span class="hljs-comment">//发送消息 </span><br>   <span class="hljs-built_in">exit</span>(<span class="hljs-number">0</span>); <br>&#125; <br><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">client</span><span class="hljs-params">()</span> </span><br><span class="hljs-function"></span>&#123; <br>   <span class="hljs-type">int</span> pid,msgqid;<span class="hljs-comment">//client的pid 和 消息队列识别码</span><br>   <span class="hljs-keyword">struct</span> <span class="hljs-title class_">msgform</span> msg;<br>   msgqid=<span class="hljs-built_in">msgget</span>(SVKEY,<span class="hljs-number">0777</span>);  <span class="hljs-comment">//打开 75#消息队列</span><br>   <br>   pid=<span class="hljs-built_in">getpid</span>();  <span class="hljs-comment">//获取client进程标识符</span><br>   *(<span class="hljs-type">int</span> *)msg.mtext=pid;  <span class="hljs-comment">//pint指针指向client进程标识符</span><br>   msg.mtype=REQ;  <span class="hljs-comment">//消息类型为 1</span><br>   <span class="hljs-comment">//printf(&quot;cli修改后的 mtype=%d，，mtext=%d\n&quot;,(int)msg.mtype,*(int *)msg.mtext); //调试信息</span><br><br>   <span class="hljs-built_in">msgsnd</span>(msgqid,&amp;msg,<span class="hljs-built_in">sizeof</span>(<span class="hljs-type">int</span>),<span class="hljs-number">0</span>);  <span class="hljs-comment">//发送消息msg入msgqid消息队列</span><br>   <span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>)&#123;<br>        <span class="hljs-built_in">msgrcv</span>(msgqid,&amp;msg,<span class="hljs-number">250</span>,pid,<span class="hljs-number">0</span>);  <span class="hljs-comment">//从队列msgqid接收消息msg </span><br>        <span class="hljs-keyword">if</span>(msg.mtype!=REQ)&#123;<br>            <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;(client):receive reply from pid=%d\n&quot;</span>,*(<span class="hljs-type">int</span> *)msg.mtext);  <span class="hljs-comment">//显示 server进程标识数</span><br>            <span class="hljs-built_in">exit</span>(<span class="hljs-number">0</span>); <br>        &#125;<br>   &#125;<br>&#125; <br><br><br><span class="hljs-type">int</span> childpid[<span class="hljs-number">10</span>]=&#123;<span class="hljs-number">-1</span>,<span class="hljs-number">-1</span>,<span class="hljs-number">-1</span>,<span class="hljs-number">-1</span>&#125;,status=<span class="hljs-number">999</span>;<br><br><span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span>&#123;<br><br>	<span class="hljs-type">int</span> flg,fd[<span class="hljs-number">2</span>];<br>	flg = <span class="hljs-built_in">pipe</span>(fd); <span class="hljs-comment">//创建管道</span><br>    <span class="hljs-keyword">if</span> (flg == <span class="hljs-number">-1</span>)<br>        <span class="hljs-built_in">perror</span>(<span class="hljs-string">&quot;pipe error&quot;</span>);<br><br>    <span class="hljs-type">int</span>  shmid,i,* addr;<span class="hljs-comment">//用于共享存储区通信</span><br><br>	childpid[<span class="hljs-number">0</span>]=<span class="hljs-built_in">getpid</span>();<span class="hljs-comment">//012分别是父进程和两个子进程的pid</span><br><br>	<span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> i=<span class="hljs-number">1</span>;i&lt;=<span class="hljs-number">2</span>;i++)&#123;<br>		<span class="hljs-type">int</span> pid=fork();<br>		<span class="hljs-keyword">if</span>(pid!=<span class="hljs-number">0</span>)<br>			childpid[i] =pid;<br>		<span class="hljs-keyword">else</span>&#123;<br>			childpid[i] =<span class="hljs-built_in">getpid</span>();<br>			<span class="hljs-keyword">break</span>;<br>		&#125;<br>	&#125;<br>    <span class="hljs-keyword">if</span>(<span class="hljs-built_in">getpid</span>()==childpid[<span class="hljs-number">0</span>])&#123;<span class="hljs-comment">//父进程</span><br><br>    &#125;<br>    <br>    <span class="hljs-keyword">if</span>(<span class="hljs-built_in">getpid</span>()==childpid[<span class="hljs-number">1</span>])&#123;<span class="hljs-comment">//子进程1</span><br>        <span class="hljs-comment">//利用管道给父进程发送消息</span><br>        <span class="hljs-built_in">close</span>(fd[<span class="hljs-number">0</span>]);<br>        <span class="hljs-type">char</span> child[<span class="hljs-number">100</span>]=<span class="hljs-string">&quot;is sending a message to parent !\0&quot;</span>;<br>        <span class="hljs-built_in">write</span>(fd[<span class="hljs-number">1</span>],child,<span class="hljs-built_in">strlen</span>(child)+<span class="hljs-number">1</span>);<br><br>        <span class="hljs-comment">//共享存储区</span><br>        shmid=<span class="hljs-built_in">shmget</span>(SHMKEY,<span class="hljs-number">512</span>,<span class="hljs-number">0777</span>|IPC_CREAT); <span class="hljs-comment">/*创建共享存储区*/</span><br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;进程%d创建了共享存储区:\n&quot;</span>,<span class="hljs-built_in">getpid</span>());<br>        addr=<span class="hljs-built_in">shmat</span>(shmid,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>);        <span class="hljs-comment">/*获取首地址*/</span><br>        *addr=<span class="hljs-number">-1</span>;<br>        <span class="hljs-keyword">while</span> (*addr == <span class="hljs-number">-1</span>);<span class="hljs-comment">//阻塞，用于进程同步</span><br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;进程%d读取共享存储区:%d\n&quot;</span>,<span class="hljs-built_in">getpid</span>(),*addr);<br>   <br>        <span class="hljs-built_in">server</span>(); <br><br>    &#125;<br>   <br>    <span class="hljs-keyword">if</span>(<span class="hljs-built_in">getpid</span>()==childpid[<span class="hljs-number">2</span>])&#123;<span class="hljs-comment">//子进程2</span><br>       <span class="hljs-comment">//接受来自子进程1 的管道消息</span><br>        <span class="hljs-built_in">close</span>(fd[<span class="hljs-number">1</span>]);<br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d  &quot;</span>,childpid[<span class="hljs-number">1</span>]);<br>        <span class="hljs-type">char</span> msg[<span class="hljs-number">100</span>];<br>        <span class="hljs-built_in">read</span>(fd[<span class="hljs-number">0</span>],msg,<span class="hljs-built_in">sizeof</span>(msg));<br>            <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%s\n&quot;</span>,msg);<br>        <br><br>        <span class="hljs-comment">//共享存储区</span><br>        shmid=<span class="hljs-built_in">shmget</span>(SHMKEY,<span class="hljs-number">512</span>,<span class="hljs-number">0777</span>);      <span class="hljs-comment">/*打开共享存储区*/</span><br>        addr=<span class="hljs-built_in">shmat</span>(shmid,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>);           <span class="hljs-comment">/*获得共享存储区首地址*/</span><br>        <span class="hljs-keyword">while</span>(*addr != <span class="hljs-number">-1</span>);<span class="hljs-comment">//阻塞，用于进程同步</span><br>        <br>        <span class="hljs-type">int</span> xie = <span class="hljs-built_in">getpid</span>();<br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;进程%d写入共享存储区:%d\n&quot;</span>,<span class="hljs-built_in">getpid</span>(),xie);<br>        *addr= xie;<br>      <br>        <span class="hljs-built_in">client</span>(); <br>    &#125;<br><br>    <span class="hljs-built_in">shmctl</span>(shmid,IPC_RMID,<span class="hljs-number">0</span>);     <span class="hljs-comment">/*撤消共享存储区，归还资源*/</span><br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br> <span class="hljs-comment">//system(&quot;ipcs  -m&quot;);//显示共享内存段的信息</span><br><span class="hljs-comment">//printf(&quot;@@@@@@@@@@@@@@@@@@@@@@&quot;);</span><br></code></pre></td></tr></table></figure>

<h2 id="运行结果："><a href="#运行结果：" class="headerlink" title="运行结果："></a>运行结果：</h2><p><img src="https://imgbed.cheney.cc/picgo/20210505112734975.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/">操作系统</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/%E7%AE%97%E6%B3%95/">算法</a>
                    
                      <a class="hover-with-bg" href="/tags/C%E8%AF%AD%E8%A8%80/">C语言</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/05/07/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F__Linux%E4%B8%8B%E5%A4%9A%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1%EF%BC%88signal%EF%BC%8Cpipe%EF%BC%89/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Linux下多进程通信（signal，pipe）</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/05/05/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F__%E8%AF%B7%E6%B1%82%E5%88%86%E9%A1%B5%E7%B3%BB%E7%BB%9F%E4%B8%AD%E7%9A%84%E7%BD%AE%E6%8D%A2%E7%AE%97%E6%B3%95(FIFO%E3%80%81LRU%E3%80%81Optimal)/">
                        <span class="hidden-mobile">请求分页系统中的置换算法(FIFO、LRU、Optimal)</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://cheney822.gitee.io/" target="_blank" rel="nofollow noopener"><span>备用网址</span></a> 
  </div>
  

  
  <!-- 备案信息 -->
  <div class="beian">
    <span>
      <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
        皖ICP备2022002876号-1
      </a>
    </span>
    
  </div>


  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
